package com.example.camera;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import com.sample.mdiafile.MediaFiles;

import android.graphics.Point;
import android.hardware.Camera;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class PreviewCallback implements Camera.PreviewCallback {
	private static final String TAG = PreviewCallback.class.getSimpleName();
	private Handler handler;
	private int message;
	private CameraConfigurationManager configManager;
	
	public PreviewCallback(CameraConfigurationManager configManager) {
		this.configManager = configManager;
	}
	@Override
	public void onPreviewFrame(byte[] data, Camera arg1) {
		// TODO Auto-generated method stub
		Log.i(TAG, "onPreViewFrame:"+(data.length/1024));
		if(data == null) {
			
		} else {
			/*File pictureFile = MediaFiles.getOutputMediaFile(MediaFiles.MEDIA_TYPE_IMAGE);
	        if (pictureFile == null){
	            Log.d(TAG, "Error creating media file, check storage permissions: " );
	            return;
	        }

	        try {
	        	
	            FileOutputStream fos = new FileOutputStream(pictureFile);
	            fos.write(data);
	            fos.close();
	        } catch (FileNotFoundException e) {
	            Log.d(TAG, "File not found: " + e.getMessage());
	        } catch (IOException e) {
	            Log.d(TAG, "Error accessing file: " + e.getMessage());
	        }*/
			Point cameraResolution = configManager.getCameraResolution();
			Message msg = this.handler.obtainMessage(message, cameraResolution.x, cameraResolution.y);
			msg.sendToTarget();
	    }
	}
	
	
	public void setHandler(Handler handler, int message) {
		this.handler = handler;
		this.message = message;
	}
}
